package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MSpecificationExcution;
import com.fortune.dto.in.mSpecification.SpecificationAddDto;
import com.fortune.dto.in.mSpecification.SpecificationEditDto;
import com.fortune.dto.in.mSpecification.SpecificationListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MSpecificationEnum;
import com.fortune.service.MSpecificationService;
import com.fortune.vo.mSpecification.MSpecification;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "product", description = "商品规格型号管理", tags = {"商品规格型号管理相关"})
@RestController
@RequestMapping("mSpecification")
public class MSpecificationController {
    @Autowired
    private MSpecificationService mSpecificationService;

    @ApiOperation(value = "商品规格型号列表", nickname = "mSpecificationList", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<MSpecification>> list(HttpServletRequest request, @ApiIgnore @Validated SpecificationListDto specificationListDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        specificationListDto.setPage((specificationListDto.getPage()-1)*10);
        List<MSpecification> list = mSpecificationService.queryMSpecificationList(companyId,specificationListDto);
        int count = mSpecificationService.queryMSpecificationListCount(companyId,specificationListDto);
        PagingData<MSpecification> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new MSpecificationExcution<PagingData<MSpecification>>(pagingData, MSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "添加商品规格型号", nickname = "mSpecificationAdd", notes = "" ,tags = {"@石匡"})
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody SpecificationAddDto specificationAddDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = mSpecificationService.queryAddMSpecificationExist(companyId,specificationAddDto);
        if (num > 0){
            return new MSpecificationExcution<Void>(null, MSpecificationEnum.EXIST);
        }
        num = mSpecificationService.addMSpecification(companyId,specificationAddDto);
        if (num < 1){
            return new MSpecificationExcution<Void>(null, MSpecificationEnum.FAILURE);
        }
        return new MSpecificationExcution<Void>(null, MSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "编辑商品规格型号", nickname = "mSpecificationEdit", notes = "" ,tags = {"@石匡"})
    @PutMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody SpecificationEditDto specificationEditDto) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = mSpecificationService.queryEditMSpecificationExist(companyId,specificationEditDto);
        if (num > 0){
            return new MSpecificationExcution<Void>(null, MSpecificationEnum.EXIST);
        }
        num = mSpecificationService.editMSpecification(companyId,specificationEditDto);
        if (num < 1){
            return new MSpecificationExcution<Void>(null, MSpecificationEnum.FAILURE);
        }
        return new MSpecificationExcution<Void>(null, MSpecificationEnum.SUCCESS);
    }

    @ApiOperation(value = "删除商品规格型号", nickname = "mSpecificationDelete", notes = "" ,tags = {"@石匡"})
    @ApiImplicitParam(name="specificationId", value="商品规格型号id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotEmpty(message="商品规格型号id不能为空") String specificationId) {
        HttpSession session = request.getSession();
        MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
        String companyId = mUserInfo.getmId();
        int num = mSpecificationService.deleteMSpecification(companyId,specificationId);
        if (num < 1){
            return new MSpecificationExcution<Void>(null, MSpecificationEnum.FAILURE);
        }
        return new MSpecificationExcution<Void>(null, MSpecificationEnum.SUCCESS);
    }
}
